/**
 * lilang
 * ProcessInstanceServiceImpl.java
 * 2013-10-25下午4:16:44
 */
package com.cq.oa.service.work.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import oa.base.service.impl.BaseServiceImpl;
import oa.base.session.SessionManger;
import oa.base.util.Constants;
import oa.base.util.DateUtil;
import oa.base.util.GenericsUtils;

import com.cq.oa.dao.wfc.ActivityInstanceDao;
import com.cq.oa.dao.wfc.ProcessInstanceDao;
import com.cq.oa.dao.wfc.TaskInstanceDao;
import com.cq.oa.domain.dto.TaskCmd;
import com.cq.oa.domain.dto.WorkDto;
import com.cq.oa.domain.wfc.Activity;
import com.cq.oa.domain.wfc.ActivityInstance;
import com.cq.oa.domain.wfc.ProcessInstance;
import com.cq.oa.domain.wfc.TaskInstance;
import com.cq.oa.service.util.ProcessUtils;
import com.cq.oa.service.work.ProcessInstanceService;

/**
 * @author lilang
 *
 */
public class ProcessInstanceServiceImpl extends BaseServiceImpl<ProcessInstance> implements ProcessInstanceService{

	
	
	private ProcessInstanceDao processInstanceDao;
	
	private TaskInstanceDao taskInstanceDao;
	
	private ActivityInstanceDao activityInstanceDao;

	public ProcessInstanceDao getProcessInstanceDao() {
		return processInstanceDao;
	}

	public void setProcessInstanceDao(ProcessInstanceDao processInstanceDao) {
		this.processInstanceDao = processInstanceDao;
	}

	public TaskInstanceDao getTaskInstanceDao() {
		return taskInstanceDao;
	}

	public void setTaskInstanceDao(TaskInstanceDao taskInstanceDao) {
		this.taskInstanceDao = taskInstanceDao;
	}

	public ActivityInstanceDao getActivityInstanceDao() {
		return activityInstanceDao;
	}

	public void setActivityInstanceDao(ActivityInstanceDao activityInstanceDao) {
		this.activityInstanceDao = activityInstanceDao;
	}

	/* (non-Javadoc)
	 * @see com.cq.oa.service.work.ProcessInstanceService#createProcessStart(com.cq.oa.domain.dto.WorkDto)
	 */
	public ProcessInstance createProcessStart(WorkDto dto,String title) {
		ProcessInstance instance = new ProcessInstance();
		//初始化数据
		this.processInstanceDao.initBase(instance);
		instance.setProcessDefinitionSid(dto.getProcessDefinition().getSid());
		instance.setBeginTime(new Date());
		instance.setBusinessId(GenericsUtils.nextRandId());
		instance.setProcessStatus(ProcessUtils.PROCESS_START);
		instance.setCname(dto.getProcessDefinition().getCname());
		instance.setTypes(dto.getProcessDefinition().getTypes());
		//业务类型
		instance.setTitle(title);
		instance.setActivitySid(dto.getStartNode().getSid());
		instance.setActivityCname(dto.getStartNode().getName());
		instance.setOperCname(SessionManger.getCname());
		this.processInstanceDao.create(instance);
		return instance;
	}

	/* (non-Javadoc)
	 * @see com.cq.oa.service.work.ProcessInstanceService#updateProcessTaskInfo(java.lang.String)
	 */
	public void updateProcessTaskInfo(String businessId) {
		ProcessInstance instance = this.findInstanceByBusinessId(businessId);
		if(null!=instance){
			// 修改信息
			
			
			this.processInstanceDao.updateSelect(instance);
		}
	}
	
	/**
	 * 通过业务号找到流程实例数据
	 * @param businessId
	 * @return
	 * lilang 
	 * 2013-10-26下午2:38:58
	 */
	private  ProcessInstance findInstanceByBusinessId(String businessId){
		Map<String,Object> params = new HashMap<String, Object>();
		params.put(Constants.BUSINESS_ID, businessId);
		List<ProcessInstance> instances=this.processInstanceDao.find(params);
		if(instances.size()>0){
			return instances.get(0);
		}else{
			return null;
		}
	}

	/* (non-Javadoc)
	 * @see com.cq.oa.service.work.ProcessInstanceService#createTask(com.cq.oa.domain.wfc.ProcessInstance)
	 */
	public TaskInstance createTask(ActivityInstance activityInstance,Activity activity,TaskCmd cmd,String title) {
		TaskInstance instance = new TaskInstance();
		//初始化数据
		this.taskInstanceDao.initBase(instance);
		//标题
		instance.setTitle(title);
		instance.setBeginTime(new Date());
		instance.setBusinessId(activityInstance.getBusinessId());
		instance.setProcessActivitySid(activityInstance.getSid());
		instance.setProcessDefinitionSid(activityInstance.getProcessDefinitionSid());
		instance.setProcessInstanceSid(activityInstance.getProcessInstanceSid());
		instance.setActivityType(String.valueOf(activity.getType()));
		instance.setCname(activity.getName());
		instance.setSendSid(SessionManger.getUserId());
		instance.setSendCname(SessionManger.getCname());
		instance.setSendDeptSid(SessionManger.getDeptId());
		instance.setSendDeptCname(SessionManger.getDeptCname());
		//发送给自己
		//instance.setOperId(SessionManger.getUserId());
		//instance.setOperCname(SessionManger.getCname());
		//instance.setOperDeptId(SessionManger.getDeptId());
		//instance.setOperDeptCname(SessionManger.getDeptCname());
		//接收人
		instance.setReciveSid(cmd.getTaskOper());
		instance.setReciveCname(cmd.getTaskCname());
		instance.setTaskStatus(ProcessUtils.TASK_START);
		//添加活动超时警告
		if(activity.getDuration()!=null){
			instance.setOvdeTime(DateUtil.addHour(new Date(), activity.getDuration().intValue()));
		}
		this.taskInstanceDao.create(instance);
		return instance;
	}

	
	public TaskInstance createHiddenTask(ActivityInstance activityInstance,Activity activity,TaskCmd cmd,String title) {
		TaskInstance instance = new TaskInstance();
		//初始化数据
		this.taskInstanceDao.initBase(instance);
		//标题
		instance.setTitle(title);
		instance.setBeginTime(new Date());
		instance.setBusinessId(activityInstance.getBusinessId());
		instance.setProcessActivitySid(activityInstance.getSid());
		instance.setProcessDefinitionSid(activityInstance.getProcessDefinitionSid());
		instance.setProcessInstanceSid(activityInstance.getProcessInstanceSid());
		instance.setActivityType(String.valueOf(activity.getType()));
		instance.setCname(activity.getName());
		instance.setSendSid(SessionManger.getUserId());
		instance.setSendCname(SessionManger.getCname());
		instance.setSendDeptSid(SessionManger.getDeptId());
		instance.setSendDeptCname(SessionManger.getDeptCname());
		//发送给自己
		//instance.setOperId(SessionManger.getUserId());
		//instance.setOperCname(SessionManger.getCname());
		//instance.setOperDeptId(SessionManger.getDeptId());
		//instance.setOperDeptCname(SessionManger.getDeptCname());
		//接收人
		instance.setReciveSid(cmd.getTaskOper());
		instance.setReciveCname(cmd.getTaskCname());
		instance.setTaskStatus(ProcessUtils.TASK_HIDDEN);
		//添加活动超时警告
		if(activity.getDuration()!=null){
			instance.setOvdeTime(DateUtil.addHour(new Date(), activity.getDuration().intValue()));
		}
		this.taskInstanceDao.create(instance);
		return instance;
	}
	
	/* (non-Javadoc)
	 * @see com.cq.oa.service.work.ProcessInstanceService#createActivityInstance(com.cq.oa.domain.wfc.ProcessInstance)
	 */
	public ActivityInstance createActivityInstance(ProcessInstance processInstance,Activity activity,Integer taskNum) {
		//如果以前有数据没完成表示多任务不生成数据
		Map<String,Object> params = new HashMap<String, Object>();
		params.put(Constants.BUSINESS_ID, processInstance.getBusinessId());
		params.put("activitySid", activity.getSid());
		params.put("activityStatus", ProcessUtils.ACTIVITY_START);
		List<ActivityInstance> list = this.activityInstanceDao.find(params);
		if(list.size()>0){
			return list.get(0);
		}
		ActivityInstance activityInstance = new ActivityInstance();
		//初始化数据
		this.activityInstanceDao.initBase(activityInstance);
		activityInstance.setActivitySid(activity.getSid());
		activityInstance.setActivityTaskNum(taskNum);
		activityInstance.setName(activity.getName());
		activityInstance.setBeginTime(new Date());
		activityInstance.setIsAsync(activity.getIsAsync());
		activityInstance.setIsMultiTask(activity.getIsMultiTask());
		activityInstance.setBusinessId(processInstance.getBusinessId());
		activityInstance.setProcessInstanceSid(processInstance.getSid());
		activityInstance.setProcessDefinitionSid(processInstance.getProcessDefinitionSid());
		activityInstance.setActivityStatus(ProcessUtils.ACTIVITY_START);
		this.activityInstanceDao.create(activityInstance);
		return activityInstance;
	}

	
	

	/* (non-Javadoc)
	 * @see com.cq.oa.service.work.ProcessInstanceService#doSaveActivity(com.cq.oa.domain.wfc.ActivityInstance)
	 */
	public ActivityInstance doSaveActivity(ActivityInstance activityInstance) {
		//activityInstance.setActivityStatus(ProcessUtils.ACTIVITY_END);
		//activityInstance.setEndTime(new Date());
		this.activityInstanceDao.create(activityInstance);
		return activityInstance;
	}

	/* (non-Javadoc)
	 * @see com.cq.oa.service.work.ProcessInstanceService#doSaveTask(java.util.List)
	 */
	public void doSaveTask(List<TaskInstance> taskInstances) {
		this.taskInstanceDao.saveAll(taskInstances);
	}

	/* (non-Javadoc)
	 * @see com.cq.oa.service.work.ProcessInstanceService#doEndTask(com.cq.oa.domain.wfc.TaskInstance)
	 */
	public boolean doEndTask(TaskInstance taskInstance) {
		this.taskInstanceDao.updateSelect(taskInstance);
		ActivityInstance activityInstance = this.activityInstanceDao.get(taskInstance.getProcessActivitySid());
		//是多任务判断做完任务的人
		if(activityInstance.getIsMultiTask()==ProcessUtils.MUST_TASK){
			Map<String,Object> param = new HashMap<String, Object>();
			param.put("processActivitySid", activityInstance.getSid());
			param.put("taskStatus", ProcessUtils.TASK_END);
			int size = this.taskInstanceDao.findByCount(param);
			//流程任务完成了
			if(size==activityInstance.getActivityTaskNum()){
				return this.doEndActivityInstance(activityInstance);
			}
			return false;
		}else{
			return this.doEndActivityInstance(activityInstance);
		}
		
		
	}
	/**
	 * 活动 实例结束
	 * @param activityInstance
	 * lilang 
	 * 2013-11-1上午9:44:13
	 */
	private boolean doEndActivityInstance(ActivityInstance activityInstance){
		activityInstance.setEndTime(new Date());
		activityInstance.setActivityStatus(ProcessUtils.ACTIVITY_END);
		this.activityInstanceDao.updateSelect(activityInstance);
		return true;
	}

	/* (non-Javadoc)
	 * @see com.cq.oa.service.work.ProcessInstanceService#doUpdateTaskStatus(java.lang.Long)
	 */
	public void doUpdateTaskStatus(Long activityInstanceId) {
		Map<String,Object> param = new HashMap<String, Object>();
		param.put("processActivitySid", activityInstanceId);
		param.put("taskStatus", ProcessUtils.TASK_HIDDEN);
		List<TaskInstance> taskInstances = this.taskInstanceDao.find(param);
		for(TaskInstance task:taskInstances){
			task.setTaskStatus(ProcessUtils.TASK_START);
		}
		taskInstanceDao.saveAll(taskInstances);
	}

	/* (non-Javadoc)
	 * @see com.cq.oa.service.work.ProcessInstanceService#getActivityInstance(java.lang.Long)
	 */
	public ActivityInstance getActivityInstance(Long sid) {
		// TODO Auto-generated method stub
		return this.activityInstanceDao.get(sid);
	}
	
}
